﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Recon.Portal.Business;
using Recon.Portal.DataObjects;

namespace Recon.Portal.WebUI
{
    public partial class DiscountPenaltyMaintenance : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (((User)Session["CurrentUserVO"]) == null)
            {
                Response.Redirect("ReconSignIn.aspx");
            }
            if (!Page.IsPostBack)
            {
                // GetUnmatchedLists();
                //FillRemitterForMatching();
                BindGrid();
                HiddenField1.Value = "";
            }
        }

        public void BindGrid()
        {
            DiscountPenaltyEntitiy discPenEntity = new DiscountPenaltyEntitiy();
            List<DiscountPenalty> discPenLst = null;
            try
            {
                if (rbOption.SelectedValue == "D")
                {
                    discPenLst = discPenEntity.GetDiscountDetails(string.Empty, ((User)Session["CurrentUserVO"]).CustomerID);
                    if (discPenLst == null)
                    {
                        gvDisc.DataSource = null;
                        gvDisc.DataBind();
                    }
                    else
                    {
                        gvDisc.DataSource = discPenLst;
                        gvDisc.DataBind();
                    }
                }
                else
                {
                    discPenLst = discPenEntity.GetPenaltyDetails(string.Empty, ((User)Session["CurrentUserVO"]).CustomerID);
                    if (discPenLst == null)
                    {
                        gvPenalty.DataSource = null;
                        gvPenalty.DataBind();
                    }
                    else
                    {
                        gvPenalty.DataSource = discPenLst;
                        gvPenalty.DataBind();
                    }
                }
               
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        //protected void Button1_Click1(object sender, EventArgs e)
        //{
        //    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>window.open('SelectDate.aspx?textbox=" + txtEffectiveDt.ClientID + "','cal','width=250,height=225,left=480,top=280')</script>");
        //}

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            DiscountPenaltyEntitiy discPenEntity = new DiscountPenaltyEntitiy();
            List<DiscountPenalty> discPenLst = null;
            System.Globalization.CultureInfo enGB = new System.Globalization.CultureInfo("en-GB");
            DiscountPenaltyEntitiy db = new DiscountPenaltyEntitiy();

            if (rbOption.SelectedValue == "D")
            {
                discPenLst = discPenEntity.GetDiscountDetails(Convert.ToString(txtCode.Text.Trim()), ((User)Session["CurrentUserVO"]).CustomerID);
            }
            else
            {
                discPenLst = discPenEntity.GetPenaltyDetails(Convert.ToString(txtCode.Text.Trim()), ((User)Session["CurrentUserVO"]).CustomerID);
            }
            if (discPenLst != null && HiddenField1.Value != "")
            {
                DiscountPenalty discPen = new DiscountPenalty
                {
                    discountPenalty = Convert.ToString(rbOption.SelectedValue.Trim() == "D" ? "D" : "P"),
                    code = Convert.ToString(txtCode.Text.Trim()),
                    customerId = Convert.ToString(((User)Session["CurrentUserVO"]).CustomerID),
                    effectiveDate = Convert.ToDateTime(txtEffectiveDt.Value),//DateTime.ParseExact(txtEffectiveDt.Value, "d-M-yyyy", enGB), 
                    daySatrt = Convert.ToInt32(txtDayStart.Text.Trim()),
                    dayEnd = Convert.ToInt32(txtDayEnd.Text.Trim()),
                    percentage = Convert.ToDouble(txtPercentage.Text.Trim()),
                    minAmount = Convert.ToDouble(txtMinAmount.Text.Trim()),
                    maxAmount = Convert.ToDouble(txtMaxAmount.Text.Trim()),
                    autoIncrementId = Convert.ToInt32(HiddenField1.Value)
                };
                int retval = db.ModifyDiscountPenalty(discPen);

                if (retval != 0)
                {
                    lblMessage.Text = rbOption.SelectedItem.Text + " modified successfully!";
                    btnClickPopup.Show();
                    ResetFields();
                }
                else
                {
                    lblMessage.Text = "Error Modifying " + rbOption.SelectedItem.Text + " !";
                    btnClickPopup.Show();
                }
            }
            else
            {
                DiscountPenalty discPen = new DiscountPenalty
                {
                    discountPenalty = Convert.ToString(rbOption.SelectedValue.Trim() == "D" ? "D" : "P"),
                    code = Convert.ToString(txtCode.Text.Trim()),
                    customerId = Convert.ToString(((User)Session["CurrentUserVO"]).CustomerID),
                    effectiveDate = Convert.ToDateTime(txtEffectiveDt.Value),//DateTime.ParseExact(txtEffectiveDt.Value, "d-M-yyyy", enGB), 
                    daySatrt = Convert.ToInt32(txtDayStart.Text.Trim()),
                    dayEnd = Convert.ToInt32(txtDayEnd.Text.Trim()),
                    percentage = Convert.ToDouble(txtPercentage.Text.Trim()),
                    minAmount = Convert.ToDouble(txtMinAmount.Text.Trim()),
                    maxAmount = Convert.ToDouble(txtMaxAmount.Text.Trim())
                };
                int retval = db.AddDiscountPenalty(discPen);

                if (retval != 0)
                {
                    lblMessage.Text = rbOption.SelectedItem.Text + " added successfully!";
                    btnClickPopup.Show();
                    ResetFields();
                }
                else
                {
                    lblMessage.Text = "Error adding " + rbOption.SelectedItem.Text + " !";
                    btnClickPopup.Show();
                }
            }
            BindGrid();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            ResetFields();
        }

        public string ConvertDate(string yyyyMMdd)
        {
            string[] str;
            string s = yyyyMMdd;
            str = s.Split('-').ToArray();

            if (str[0].Length == 1)
            { str[0] = "0" + str[0]; }

            if (str[1].Length == 1)
            { str[1] = "0" + str[1]; }

            s = str[2] + "-" + str[1] + "-" + str[0];
            return s;

        }

        private void ResetFields()
        {
            rbOption.SelectedValue = "D";
            txtEffectiveDt.Value = "";
            txtDayStart.Text = "";
            txtDayEnd.Text = "";
            txtPercentage.Text = "";
            txtMinAmount.Text = "";
            txtMaxAmount.Text = "";
            txtCode.Text = "";
            HiddenField1.Value = "";
            pnlDisc.Visible = true;
            pnlPenalty.Visible = false;
            btnSubmit.Text = "Add";
        }

        private void FillRemitterForMatching()
        {
            CustomerEntity db = new CustomerEntity();
            List<Customer> dtRemitter = db.GetCustomer(string.Empty);

            if (dtRemitter != null && dtRemitter.Count > 0)
            {
                if (dtRemitter != null && dtRemitter.Count<Customer>() > 0)
                {
                    foreach (Customer remitter in dtRemitter)
                    {
                        //ddlCustomer.Items.Add(new ListItem(remitter.CustomerID + " - " + remitter.CustomerName, remitter.CustomerID));
                    }
                }
            }
        }
        protected void btnok_Click(object sender, EventArgs e)
        {
            btnClickPopup.Hide();
        }

        protected void rbOption_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (rbOption.SelectedValue == "D")
            {
                pnlDisc.Visible = true;
                pnlPenalty.Visible = false;
            }
            else
            {
                pnlDisc.Visible = false;
                pnlPenalty.Visible = true;
            }
            HiddenField1.Value = "";
            BindGrid();
        }

        protected void gvDisc_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex > -1)
            {
                //double value = 0.00;
                //if (e.Row.Cells[4].Text != "" && e.Row.Cells[4].Text != "&nbsp;")
                //{
                //    value = Convert.ToDouble(e.Row.Cells[4].Text);
                //    e.Row.Cells[4].Text = value.ToString("#,#.00", CultureInfo.InvariantCulture);
                //    e.Row.Cells[4].HorizontalAlign = HorizontalAlign.Right;
                //}
                //else
                //{
                //    e.Row.Cells[4].Text = "0.00";
                //}
                DateTime dt;
                Label dat = ((Label)e.Row.FindControl("lbleffectiveDate"));
                if (dat.Text != "" && dat.Text != "&nbsp;")
                {
                    dt = Convert.ToDateTime(dat.Text);
                    e.Row.Cells[2].Text = dt.ToString("dd-MM-yyyy");
                    e.Row.Cells[2].HorizontalAlign = HorizontalAlign.Center;
                }
                else
                {
                    e.Row.Cells[2].Text = "";
                }
            }
        }

        protected void gvPenalty_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex > -1)
            {
                //double value = 0.00;
                //if (e.Row.Cells[4].Text != "" && e.Row.Cells[4].Text != "&nbsp;")
                //{
                //    value = Convert.ToDouble(e.Row.Cells[4].Text);
                //    e.Row.Cells[4].Text = value.ToString("#,#.00", CultureInfo.InvariantCulture);
                //    e.Row.Cells[4].HorizontalAlign = HorizontalAlign.Right;
                //}
                //else
                //{
                //    e.Row.Cells[4].Text = "0.00";
                //}
                DateTime dt;
                Label dat = ((Label)e.Row.FindControl("lblPenaltyeffectiveDate"));
                if (dat.Text != "" && dat.Text != "&nbsp;")
                {
                    dt = Convert.ToDateTime(dat.Text);
                    e.Row.Cells[2].Text = dt.ToString("dd-MM-yyyy");
                    e.Row.Cells[2].HorizontalAlign = HorizontalAlign.Center;
                }
                else
                {
                    e.Row.Cells[2].Text = "";
                }
            }
        }
    }
}